% GAUTIER LE BIHAN - 2020
% Replication files for �Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model� �Review of Economics and Statistics
%
% Appendix Table A

clear all
close all
clear matrix
clc


addpath('..\..\Utilities')  

cd ..\..\Simu_identification\Table_ident_KR\constrained_model\simul_CalvoP
param0=[0.0503 0.0303 0.0344   0.6946];
    p0=param0(1);
    mu_c=param0(2);
    sig_eps_a=param0(3);
    rho_a=0.6946;   

    weight_j=0.57;    
   [f2, avfracup2,    med2,    interq2, kur2]=geNCalvoPlus_SMM(p0, mu_c, sig_eps_a, rho_a, weight_j);
 
   m_model=[f2; avfracup2;    med2;    interq2; kur2]
 
   cd ..\..\Simu_identification\Table_ident_KR\constrained_model\simul_CalvoP


   param0=[  atanh(0.2751) ]; 
   actual_moments=m_model;          
   actual_std=[ 0.0008  0.0042  0.0005  0.0010  0.1057]

mu=(actual_moments(1)-0.0503)*(0.0303)
actual_var= actual_std.* actual_std*1000;
%

for jj=1:1;
    
p_a0 =param0(jj,1);%param0(jj,4);
p_a_init = p_a0;


% Adjustement of initial search area (beyond 5 percent)

%power_p0 = 1.02;
power_mu_c = 1.0;
power_sig_eps_a = 1.0;
power_p_a = 1.0;

% save SMM

% create vector for initial parameter values
vec0 = [ p_a0];

% Keep track of results
conv_SMM = zeros(0,3);
xopt_SMM = 100000; % start iteration
conv_opt = zeros(3,6);
SMM_count = 0; % counter

% tolerance level
tol_SMM = 1e-3;




save('SMM','vec0','p_a0',...
    'p_a_init',...
    'power_p_a',...
    'conv_SMM','xopt_SMM','conv_opt','SMM_count',...
    'tol_SMM','-append')
% 'power_phi','phi_init','phi0'

%% Options
%  Display, TolX, TolFun, MaxFunEvals

%% Set calibration target
% std.Y, autocorr(Y), frequency of default:
moment1 = actual_moments(1);
moment2 = actual_moments(2);
moment3 = actual_moments(3);
moment4 = actual_moments(4); 
moment5 = actual_moments(5);

weight_j=0.57;



%moment6 = actual_moments(jj,7);
%moment7 = actual_moments(jj,3);


target = [moment1; moment2; moment3; moment4; moment5];
save('SMM','target','-append')
%% Start minimization routine 
% options = optimset('MaxIter',20);%, 'TolX', 1e-4,'Tolfun', 1e-3);

scale=[actual_var(jj,:)'];
save('SMM','target','scale','-append')
%% Start minimization routine 
 %options = optimset('MaxIter',20);%, 'TolX', 1e-4,'Tolfun', 1e-3);
 %options = optimset('TolX', 1e-4,'Tolfun', 1e-3);
options = optimset('MaxFunEvals',80);

[x,fval, exiflag] = fminsearch(@SMMmain,vec0, options);
res_ref2(jj,:)=[x' fval exiflag target'];

  save res_ref2 res_ref2;
end